<?php

namespace app\common\model\station;

use think\Model;

class BabycarStation extends Model
{
    protected $name = 'babycar_station';
    protected $pk = 'station_id';

    /**
     * 获取站点列表
     * @param array $params 查询参数
     * @return array
     */
    public function getList($params = [])
    {
        $where = [];
        
        // 只查询未删除的记录
        $where[] = ['is_delete', '=', 0];
        
        // 按状态筛选
        if (isset($params['state']) && in_array($params['state'], ['active', 'inactive', 'under_maintenance'])) {
            $where[] = ['state', '=', $params['state']];
        }
        
        // 按名称搜索
        if (!empty($params['title'])) {
            $where[] = ['title', 'like', '%' . $params['title'] . '%'];
        }

        // 构建查询
        $query = $this->where($where);
        
        // 添加排序
        $query->order('add_date', 'desc');
        
        // 执行分页查询
        $list = $query->paginate([
            'list_rows' => 10,
            'query' => $params
        ]);

        return $list;
    }

    /**
     * 获取站点详情
     * @param int $station_id
     * @return array|null
     */
    public function getDetail($station_id)
    {
        return $this->where([
            'station_id' => $station_id,
            'is_delete' => 0
        ])->find();
    }

    /**
     * 创建站点
     * @param array $data
     * @return bool
     */
    public function createStation($data)
    {
        return $this->save($data);
    }

    /**
     * 更新站点信息
     * @param int $station_id
     * @param array $data
     * @return bool
     */
    public function updateStation($station_id, $data)
    {
        return $this->where('station_id', $station_id)->update($data);
    }

    /**
     * 软删除站点
     * @param int $station_id
     * @return bool
     */
    public function deleteStation($station_id)
    {
        return $this->where('station_id', $station_id)->update(['is_delete' => 1]);
    }
} 